<template>
  <div>
    <div class="dashboard-container">
      <div class="app-container">
        <el-card class="tree-card">
          <!-- 用了一个行列布局 -->
          <tree-tools :tree-data="company" :is-root="true" @openDialog="openDialog" />
          <!-- 树组件区域 -->
          <el-tree
            :data="data"
            :props="{ label: 'name', children: 'children' }"
          >
            <!-- 复用头部组件 -->
            <tree-tools slot-scope="{data}" :tree-data="data" @openDialog="openDialog" @updateList="initData" @editDialog="editDialog" />
          </el-tree>
        </el-card>
      </div>
    </div>
    <addeptVue ref="addDept" @updateList="initData" />
  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils'
import { getDepartmentListApi } from '@/api/departments'
import TreeTools from '@/views/components/tree-tools.vue'
import addeptVue from '@/views/components/add-dept.vue'
export default {
  name: 'Departments',
  components: {
    TreeTools,
    addeptVue
  },
  data() {
    return {
      company: { name: '传智教育', manager: '负责人', id: '' },
      data: []

    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      const { companyName, depts } = await getDepartmentListApi()
      this.company.name = companyName
      this.data = tranListToTreeData(depts, '')
    },
    openDialog(val) {
      console.log('我们要给id为' + val + '的部门新增子部门')
      // 通过ref来获取到组件的实例对象，直接修改里面的数据
      // this.dialogShow = true
      this.$refs.addDept.dialogShow = true
      this.$refs.addDept.formData.pid = val
    },
    editDialog(val) {
      console.log('我要修改' + val)
      this.$refs.addDept.getFormDataInfo(val)
    }

  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
